package com.ikun.yummy.activitys.main.fragment.follow;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.ikun.yummy.App;
import com.ikun.yummy.R;
import com.ikun.yummy.activitys.person.PersonActivity;
import com.ikun.yummy.adapter.FollowListAdapter;
import com.ikun.yummy.bean.FollowInfo;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.manager.FollowOPManager;
import com.ikun.yummy.manager.UserOpManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class FollowFragment extends Fragment {
    private TabLayout tabbar;
    private ListView listView;
    private List<FollowInfo> followList = new ArrayList<>();
    private FollowListAdapter adp;
    private FollowOPManager followOPManager;
    private UserOpManager userOpManager;
    private UserInfo userInfo;
    private String user;
    
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_follow, container, false);
        initview(root);
        return root;
    }
    private void initview(View root) {
        tabbar=root.findViewById(R.id.follow_tab);
        listView=root.findViewById(R.id.follow_list);
        adp=new FollowListAdapter(requireContext(), R.layout.item_follow, followList, (v, followInfo) -> {
            if (followInfo.type==1){//取关
                followOPManager.delete(userInfo.username,followInfo.othername);
                loadList(1);
            }else{//回关
                FollowInfo followInfo1 = followOPManager.queryFollowerSingle(followInfo.username, followInfo.othername);
                if (followInfo1==null){//没回关就回关
                    if (userInfo.username.equals(followInfo.othername)){
                        Toast.makeText(requireContext(), "不能关注自己哦", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    followOPManager.insertFollower(new FollowInfo("",userInfo.username,followInfo.othername,1));
                    followOPManager.insertFollowee(new FollowInfo("",followInfo.othername,userInfo.username,1));
                }else {//回关了取消关注粉丝
                    followOPManager.delete(userInfo.username,followInfo.othername);
                }
                loadList(2);
            }

        });
        listView.setAdapter(adp);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            FollowInfo bean = followList.get(position);
            Intent intent=new Intent(requireContext(), PersonActivity.class);
            intent.putExtra("username",bean.othername);
            startActivity(intent);
        });

        followOPManager=new FollowOPManager(requireContext());
        userOpManager=new UserOpManager(requireContext());
        user= App.sp.getString("username","null");
        userInfo=userOpManager.queryUser(user);

        tabbar.addTab(tabbar.newTab().setText("我的关注"));
        tabbar.addTab(tabbar.newTab().setText("我的粉丝"));
        tabbar.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (Objects.requireNonNull(tab.getText()).toString().equals("我的关注")){
                    loadList(1);
                }else{
                    loadList(2);
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }
            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });
        loadList(1);
    }
    private void loadList(int i) {
        if (!user.equals("null")&&userInfo.loginstate.equals("1")){
            followList.clear();
            if (i==1){
                List<FollowInfo> list = followOPManager.queryFollower(userInfo.username);
                if (list!=null){
                    for (FollowInfo follow :list) {
                        follow.type=1;
                        followList.add(follow);
                    }
                }
            }else{
                List<FollowInfo> list = followOPManager.queryFollowee(userInfo.username);
                if(list!=null){
                    for (FollowInfo follow :list) {
                        if (follow.username.equals(userInfo.username)){
                            follow.type=2;
                            followList.add(follow);
                        }
                    }
                }
            }
            adp.notifyDataSetChanged();
        }else{
            Toast.makeText(requireContext(), "请先登录", Toast.LENGTH_SHORT).show();
        }
    }
}